package org.wenhua.yuan.rn

import android.os.Bundle
import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity
import com.facebook.react.ReactInstanceManager
import com.facebook.react.ReactPackage
import com.facebook.react.ReactRootView
import com.facebook.react.common.LifecycleState
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler
import com.facebook.react.shell.MainReactPackage
import org.wenhua.yuan.BuildConfig
import org.wenhua.yuan.and.module_rn.JReactPackage
import java.util.*

class RNContainerActivity : AppCompatActivity(), DefaultHardwareBackBtnHandler {

    private var mReactRootView : ReactRootView? = null
    private var mReactInstanceManager: ReactInstanceManager? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        ImmersiveStatusBar.setStatusBar(this, true, resources.getColor(R.color.colorPrimary), true)
//        setContentView(R.layout.activity_r_n_container)

        val packages = ArrayList<ReactPackage>()
        packages.add(MainReactPackage())
        packages.add(JReactPackage())

        mReactRootView = ReactRootView(this)
        mReactInstanceManager = ReactInstanceManager.builder()
            .setApplication(application)
            .setCurrentActivity(this)
            .setBundleAssetName("rn/index.android.bundle")
            .setJSMainModulePath("index")
            .addPackages(packages)
            .setUseDeveloperSupport(BuildConfig.DEBUG)
            .setInitialLifecycleState(LifecycleState.RESUMED)
            .build()

        var bundle = Bundle()
        bundle.putString("goto", "rn-location")
        // moduleName 是在rn中index.js注册的appName
        // 可由此添加更多React Native的组件，即多个RN入口
        mReactRootView!!.startReactApplication(mReactInstanceManager, "hibrid_rn", bundle)

        setContentView(mReactRootView) // 这里将rn作为整个view铺满屏幕
    }

    override fun invokeDefaultOnBackPressed() {
        super.onBackPressed()
    }


    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_MENU) { //Ctrl + M 打开RN开发者菜单
            mReactInstanceManager!!.showDevOptionsDialog()
            return true
        }
        return super.onKeyUp(keyCode, event)
    }

    override fun onPause() {
        super.onPause()
        if (mReactInstanceManager != null) {
            mReactInstanceManager!!.onHostPause(this)
        }
    }

    override fun onResume() {
        super.onResume()
        if (mReactInstanceManager != null) {
            mReactInstanceManager!!.onHostResume(this, this)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mReactInstanceManager != null) {
            mReactInstanceManager!!.onHostDestroy(this)
        }
        if (mReactRootView != null) {
            mReactRootView!!.unmountReactApplication()
        }
    }

    override fun onBackPressed() {
        if (mReactInstanceManager != null) {
            mReactInstanceManager!!.onBackPressed()
        } else {
            super.onBackPressed()
        }
    }


}
